home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / VideoToolbox / VideoToolboxSources / SndPlay1.c < prev    next >
Text File  |  1993-07-09  |  3KB  |  94 lines

  1. /* 
  2. SndPlay1.c
  3.  
  4. SndPlay1(snd) plays a sound, asynchronously, i.e. it returns immediately, while the
  5. sound is still playing. The argument is a handle to a snd resource. If a sound is 
  6. still playing from a previous call to SndPlay1, it is allowed to finish before
  7. closing and reopening the channel and beginning the new sound.
  8. SndPlay1(NULL) waits for the sound to end and then closes the channel.
  9. SndStop1() closes the channel immediately.
  10. SndDone1() returns true once the last sound initiated by SndPlay1() has 
  11. finished.
  12.  
  13. GENERAL NOTE:
  14. The Apple Sound Manager has the annoying characteristic of insisting on loading
  15. any synth that's mentioned in a snd resource, even if that synth is already loaded,
  16. which causes an error. This makes it necessary to dispose of and recreate the 
  17. snd channel before each new sound, which these routines do. Apple advises against 
  18. this approach because it leaves the channel open a lot of the time, 
  19. which blocks SysBeep. 
  20.  
  21. To avoid this, make sure to call SndPlay1(NULL) or SndStop1() to close the channel after
  22. you start a sound going.
  23.  
  24. The easiest way to get a sound to play is to call GetNamedResource(). 
  25.     Handle snd;
  26.     snd=GetNamedResource('snd ',"\pSimple Beep");
  27. However, it's also easy to create your own snd in memory, as a series of commands,
  28. following the instructions in Inside Mac VI.
  29.  
  30. HISTORY:
  31. 3/30/92    dgp wrote it.
  32. 4/1/92    dgp    renamed it and commented out the printf's.
  33. 7/9/93    dgp Test MATLAB in if() instead of #if. 
  34. */
  35. #include "VideoToolbox.h"
  36. #include <Sound.h>
  37. static pascal void SndCallBack(SndChannelPtr channel,SndCommand command);
  38. static SndChannelPtr channel=NULL;
  39. static short soundDone;
  40.  
  41. OSErr SndPlay1(Handle snd)
  42. {
  43.     OSErr error=0;
  44.     SndCommand callBack;
  45.     static firstTime=1;
  46.  
  47.     if(firstTime){
  48.         if(!MATLAB)_atexit(SndStop1);
  49.         firstTime=0;
  50.     }
  51.     if(channel!=NULL)error=SndDisposeChannel(channel,FALSE);    // wait till done
  52.     channel=NULL;
  53.     if(error)return error;
  54.     if(snd==NULL)return 0;
  55.     error=SndNewChannel(&channel,0,0L,SndCallBack);
  56.     if(error){
  57. //        printf("SndPlay1:SndNewChannel failed with error %d\n",error);
  58.         return error;
  59.     }
  60.     error=SndPlay(channel,snd,TRUE);
  61.     if(error){
  62. //        printf("SndPlay1:SndPlay failed with error %d\n",error);
  63.         return error;
  64.     }
  65.     soundDone=0;
  66.     callBack.cmd=callBackCmd;
  67.     callBack.param1=1;
  68.     callBack.param2=(long)&soundDone;
  69.     error=SndDoCommand(channel,&callBack,FALSE);
  70. //    if(error)printf("SndPlay1:SndDoCommand failed with error %d\n",error);
  71.     return error;
  72. }
  73.  
  74. void SndStop1(void)
  75. {
  76.     if(channel!=NULL)SndDisposeChannel(channel,TRUE);        // immediately
  77.     channel=NULL;
  78. }
  79.  
  80. short SndDone1(void)
  81. // Returns true once the last sound initiated by SndPlay1 has 
  82. // finished.
  83. {
  84.     return soundDone;
  85. }
  86.  
  87. #pragma options(!profile)    // Disable profiling because A5 may be invalid.
  88.  
  89. static pascal void SndCallBack(SndChannelPtr channel,SndCommand command)
  90. // Load a short int.
  91. // Called back by sound manager.  Lets us know when sound is done.
  92. {
  93.     if(command.param2 != 0L) *(short *)command.param2=command.param1;
  94. }